#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QColorDialog>
#include "qgraphicdialog.h"
#include <QTextStream>
#include <QStyle>
#include <QRegExp>
#include <QFileDialog>
#include <QErrorMessage>
#include <QtSvg/QSvgGenerator>
#include <QPainter>
#include "qpaintutils.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    color = Qt::black;
    textValidationFailed = false;
    diameterValidationFailed = false;

    renderArea = new QRenderArea;
    renderArea->setVisible(false);

    textLabel = new QLabel("Text: ");
    textEdit = new QLineEdit;
    textLabel->setBuddy(textEdit);

    diameterLabel = new QLabel("Diameter: ");
    diameterEdit = new QLineEdit;
    diameterLabel->setBuddy(diameterEdit);

    textErrorMessage = new QLabel;
    textErrorMessage->setStyleSheet("color: red");
    textErrorMessage->setVisible(false);

    diameterErrorMessage = new QLabel;
    diameterErrorMessage->setStyleSheet("color: red");
    diameterErrorMessage->setVisible(false);

    selectColorButton = new QPushButton("Select color");

    buttonBox = new QDialogButtonBox;
    buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);

    mainLayout = new QGridLayout;

    mainLayout->setColumnStretch(0, 1);
    mainLayout->setColumnStretch(3, 1);

    mainLayout->addWidget(renderArea, 0, 0, 1, 4);
    mainLayout->addWidget(textLabel, 2, 0, Qt::AlignCenter);
    mainLayout->addWidget(textEdit, 2, 1);
    mainLayout->addWidget(textErrorMessage, 3, 1);
    mainLayout->addWidget(diameterLabel, 4, 0, Qt::AlignCenter);
    mainLayout->addWidget(diameterEdit, 4, 1);
    mainLayout->addWidget(diameterErrorMessage, 5, 1);
    mainLayout->addWidget(selectColorButton, 6, 0, 1, 4);
    mainLayout->addWidget(buttonBox, 7, 0, 1, 3);

    connect(selectColorButton, &QPushButton::clicked, this, &MainWindow::on_selectColorButton_clicked);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &MainWindow::on_buttonBox_accepted);
    connect(ui->actionSave_as, &QAction::triggered, this, &MainWindow::onSaveAs);

    ui->centralWidget->setLayout(mainLayout);

    setWindowTitle("Maksymenko laba3");
}

MainWindow::~MainWindow()
{
    delete buttonBox;
    delete selectColorButton;
    delete diameterErrorMessage;
    delete diameterEdit;
    delete diameterLabel;
    delete textErrorMessage;
    delete textEdit;
    delete textLabel;
    delete renderArea;
    delete mainLayout;
    delete ui;
}

void MainWindow::on_selectColorButton_clicked()
{
    QColor color = QColorDialog::getColor(Qt::black, this);
    if(color.isValid()) this->color = color;
}

void MainWindow::on_buttonBox_accepted()
{
    openDialogAndDrawOnMainWindow();
}

void MainWindow::on_actionMy_dialog_triggered()
{
    openDialogAndDrawOnMainWindow();
}

void MainWindow::openDialogAndDrawOnMainWindow()
{
    openDialog();

    int d  = diameterEdit->text().toInt();

    mainLayout->setColumnMinimumWidth(0, d + 10);
    mainLayout->setRowMinimumHeight(0, d + 10);

    renderArea->setVisible(true);
    renderArea->setDiameter(d);
    renderArea->setColor(&this->color);
    renderArea->setText(textEdit->text());
    renderArea->draw(true);
}

void MainWindow::openDialog()
{
    if(isValid())
    {
        QGraphicDialog dialog(this, &color, textEdit->text(), diameterEdit->text());
        dialog.exec();
    }
}

void MainWindow::onSaveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this, "Save html file and image", QDir::homePath(), "HTML file *.html (*.html)");

    if(!fileName.isEmpty())
    {
        QFile::copy(QFile(":/html/index.html").fileName(), fileName);
        QFileInfo fileInfo(fileName);

        QFile imgFile(fileInfo.path() + "/result.svg");

        if(!imgFile.open(QIODevice::WriteOnly))
        {
            QErrorMessage msg;
            msg.showMessage("Unable to create .svg file");
        }
        else
        {
            QSvgGenerator generator;
            generator.setSize(this->renderArea->size());
            generator.setOutputDevice(&imgFile);
            QPainter painter;
            painter.begin(&generator);

            QPoint center(width() / 2, height() /2);

            QPaintUtils::drawCircle(&painter, &center, diameterEdit->text().toInt(), textEdit->text(), 20, &color);

            painter.end();
            imgFile.close();
        }
    }

}

bool MainWindow::isValid()
{
    if (textEdit->text().isEmpty())
    {
        textErrorMessage->setVisible(true);
        textEdit->setStyleSheet("border: 1px solid red");
        textValidationFailed = true;
        return false;
    }
    else
    {
        textErrorMessage->setVisible(false);
        textErrorMessage->setStyleSheet("border: 1px solid");
        textValidationFailed = false;
    }

    if (diameterEdit->text().isEmpty())
    {
        diameterErrorMessage->setText("Diameter can not be empty!");
        diameterEdit->setStyleSheet("border: 1px solid red");
        diameterErrorMessage->setVisible(true);
        diameterValidationFailed = true;
        return false;
    }
    else if (!QRegExp("\\d*").exactMatch(diameterEdit->text()))
    {
        diameterErrorMessage->setText("Diameter must be a number!");
        diameterEdit->setStyleSheet("border: 1px solid red");
        diameterErrorMessage->setVisible(true);
        diameterValidationFailed = true;
        return false;
    }
    else
    {
        diameterErrorMessage->setVisible(false);
        diameterEdit->setStyleSheet("border: 1px solid");
        diameterValidationFailed = false;
    }
    return true;
}
